package cn.sanshuilin.kst.system.provider.write.dao;


import cn.sanshuilin.kst.common.utils.LogPrinter;
import cn.sanshuilin.kst.common.utils.PagingData;
import cn.sanshuilin.kst.common.utils.Utility;
import cn.sanshuilin.kst.system.provider.bean.Attention;
import cn.sanshuilin.kst.system.provider.bean.Member;
import cn.sanshuilin.kst.system.provider.core.CurrentInfo;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import java.util.List;

/**
 * @author greenlee
 * @version v1.0
 * @create 创建时间: 2017-11-29 14:35
 * Copyright 2017  by 三水林科技有限公司
 **/
@Repository
public class AttentionReq extends AbstractJpaDao<Attention> {

    public List<Member> getSellersByLoc(int nPageIndex, int nPageSize, String location, int heat) {

            String hql = "SELECT a FROM Member a WHERE a.AirPort= :AirPort ";
            if(heat!=-1){
                hql+=" ORDER BY (a.FanCount) DESC";
            }
            else{
                hql+=" ORDER BY a.Name";
            }
            Query query = entityManager.createNativeQuery(hql,Attention.class);
            query.setParameter("AirPort", location);
            query.setFirstResult(nPageIndex * nPageSize);
            query.setMaxResults(nPageSize);
            List<Member> list = query.getResultList();
        return list;
    }

    public List<Attention> getAttentionSubjectsByPubMemberID(int nPageIndex,int nPageSize,String pubMemberID) {

        PagingData pagingData =  new PagingData();

            String hql = "SELECT a FROM Attention a,Subject s WHERE  a.PubMemberID= :PubMemberID "
                    + "AND a.ObjID=s.ID ORDER BY a.PubDate";

            Query query = entityManager.createNativeQuery(hql,Attention.class);
            query.setParameter("PubMemberID", CurrentInfo.getCurrentMember().getId());
            query.setFirstResult(nPageIndex * nPageSize);
            query.setMaxResults(nPageSize);
            List<Attention> list = query.getResultList();


        return list;
    }

    public List<Attention> getAttentionMembersNoExtendByPubMemberID(int nPageIndex,int nPageSize,String pubMemberID) {

            String hql = "SELECT a FROM Attention a,Member s WHERE  a.PubMemberID= :PubMemberID "
                    + "AND a.ObjID=s.ID  ORDER BY a.PubDate";
            Query query = entityManager.createNativeQuery(hql,Attention.class);
            query.setParameter("PubMemberID", CurrentInfo.getCurrentMember().getId());
            query.setFirstResult(nPageIndex * nPageSize);
            query.setMaxResults(nPageSize);
            List<Attention> list = query.getResultList();
        return list;
    }



    public List<Attention> getAttentionMembersByPubMemberID(int nPageIndex, int nPageSize, String pubMemberID) {

        PagingData pagingData = new PagingData();

        String hql = "SELECT a FROM Attention a,Member s WHERE  a.PubMemberID= :PubMemberID "
                + "AND a.ObjID=s.ID  ORDER BY a.PubDate";
        Query query = entityManager.createNativeQuery(hql, Attention.class);
        query.setParameter("PubMemberID", CurrentInfo.getCurrentMember().getId());
        query.setFirstResult(nPageIndex * nPageSize);
        query.setMaxResults(nPageSize);
        List<Attention> list = query.getResultList();
        LogPrinter.info("canAttention0001212" + list.size());

        return list;
    }


    public List<Attention> getAttentionSharesByPubMemberID(int nPageIndex, int nPageSize,
                                                           String pubMemberID, int searchCategoryID) {
        PagingData pagingData = new PagingData();

        String hql = "SELECT a FROM Attention a,Share s WHERE  a.PubMemberID= :PubMemberID "
                + "AND a.ObjID=s.ID AND s.CategoryID= :searchCategoryID ORDER BY a.PubDate";

        Query query = entityManager.createNativeQuery(hql, Attention.class);
        query.setParameter("PubMemberID", CurrentInfo.getCurrentMember().getId());
        query.setParameter("searchCategoryID", searchCategoryID);

        if (nPageIndex != -1 && nPageSize != -1) {
            query.setFirstResult(nPageIndex * nPageSize);
            query.setMaxResults(nPageSize);
        }
        List<Attention> list = query.getResultList();

        return list;
    }


    public List<Attention> getAttentionsNoExtendByObjID(int nPageIndex, int nPageSize, String strObjID) {
        String hql = "FROM Attention a WHERE a.ObjID= :ObjID ORDER BY a.PubDate";
        Query query = entityManager.createNativeQuery(hql, Attention.class);
        query.setParameter("ObjID", strObjID);

        if (nPageIndex != -1 && nPageSize != -1) {
            query.setFirstResult(nPageIndex * nPageSize);
            query.setMaxResults(nPageSize);
        }
        List<Attention> list = query.getResultList();

        return list;
    }

    public List<Attention> getAttentionsByObjID(int nPageIndex, int nPageSize, String strObjID) {
        String hql = "FROM Attention a WHERE a.ObjID= :ObjID ORDER BY a.PubDate";
        Query query = entityManager.createNativeQuery(hql, Attention.class);
        query.setParameter("ObjID", strObjID);
        if (nPageIndex != -1 && nPageSize != -1) {
            query.setFirstResult(nPageIndex * nPageSize);
            query.setMaxResults(nPageSize);
        }
        List<Attention> list = query.getResultList();
        return list;
    }


    public String updateCount(String ID, int count) {
        Query query = entityManager.createNativeQuery("SELECT Name FROM TableIndex  WHERE ID = :ID");
        query.setParameter("ID", ID);
        String strTableName = Utility.toSafeString(query.getSingleResult());
        LogPrinter.info("updateCountupdateCountupdateCountupdateCount");
        return strTableName;
    }


}
